package com.building.hcrm.ui.customer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.R.integer;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.building.hcrm.R;
import com.building.hcrm.R.drawable;
import com.building.hcrm.R.id;
import com.building.hcrm.R.layout;
import com.building.hcrm.R.string;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseCustomerActivity;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.model.CustomerDetail;
import com.widget.time.JudgeDate;
import com.widget.time.ScreenInfo;
import com.widget.time.WheelMain;

public class CustomerClassify extends BaseCustomerActivity {

	private WheelMain wheelMain;
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	private ImageView ivClassify1;
	private ImageView ivClassify2;
	private ImageView ivClassify3;
	private ImageView ivClassify4;
	private ImageView ivClassify5;
	private ImageView ivClassify6;
	private ImageView ivClassify7;
	private ImageView ivClassify8;
	private ImageView ivClassify9;
	private ImageView ivClassify10;
	private ImageView ivClassify11;
	private RelativeLayout linearClassify4Time;
	private RelativeLayout linearClassify5PayTime;
	private RelativeLayout linearClassify5NextPayTime;

	private ImageView ivDatePicker1;
	private ImageView ivDatePicker2;
	private ImageView ivDatePicker3;

	private EditText etClassify4Time;
	private EditText etClassify5PayTime;
	private EditText etClassify5NextPayTime;
	private int mActivityStatus = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layout.partial_customer_classify);
		initControls();
		setOnAllListeners();
		initDatas();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			imgbtnHome.performClick();
		}
		return false;

	}

	private void initControls() {
		ivClassify1 = (ImageView) findViewById(id.iv_classify1);
		ivClassify2 = (ImageView) findViewById(id.iv_classify2);
		ivClassify3 = (ImageView) findViewById(id.iv_classify3);
		ivClassify4 = (ImageView) findViewById(id.iv_classify4);
		ivClassify5 = (ImageView) findViewById(id.iv_classify5);
		ivClassify6 = (ImageView) findViewById(id.iv_classify6);
		ivClassify7 = (ImageView) findViewById(id.iv_classify7);
		ivClassify8 = (ImageView) findViewById(id.iv_classify8);
		ivClassify9 = (ImageView) findViewById(id.iv_classify9);
		ivClassify10 = (ImageView) findViewById(id.iv_classify10);
		ivClassify11 = (ImageView) findViewById(id.iv_classify11);

		ivDatePicker1 = (ImageView) findViewById(id.iv_date_picker1);
		ivDatePicker2 = (ImageView) findViewById(id.iv_date_picker2);
		ivDatePicker3 = (ImageView) findViewById(id.iv_date_picker3);

		linearClassify4Time = (RelativeLayout) findViewById(id.linear_classify4_time);
		linearClassify5PayTime = (RelativeLayout) findViewById(id.linear_classify5_pay_time);
		linearClassify5NextPayTime = (RelativeLayout) findViewById(id.linear_classify5_next_pay_time);

		etClassify4Time = (EditText) findViewById(R.id.et_classify4_time);
		etClassify5PayTime = (EditText) findViewById(R.id.et_classify5_pay_time);
		etClassify5NextPayTime = (EditText) findViewById(R.id.et_classify5_next_pay_time);

		imgbtnHome = (ImageButton) findViewById(id.imgbtn_home);
		imgbtnHome.setVisibility(View.VISIBLE);
	}

	private void setOnAllListeners() {
		setEditTextForbiddenVirtualKeyBoard(etClassify4Time);
		setEditTextForbiddenVirtualKeyBoard(etClassify5PayTime);
		setEditTextForbiddenVirtualKeyBoard(etClassify5NextPayTime);

		setTimeClickListener(ivDatePicker1);
		setTimeClickListener(ivDatePicker2);
		setTimeClickListener(ivDatePicker3);

		setImageViewListener(ivClassify1);
		setImageViewListener(ivClassify2);
		setImageViewListener(ivClassify3);
		setImageViewListener(ivClassify4);
		setImageViewListener(ivClassify5);
		setImageViewListener(ivClassify6);
		setImageViewListener(ivClassify7);
		setImageViewListener(ivClassify8);
		setImageViewListener(ivClassify9);
		setImageViewListener(ivClassify10);
		setImageViewListener(ivClassify11);

		imgbtnHome.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getDatas();
				Intent intent = new Intent();
				intent.putExtra(ComConst.CUSTOMER_DATAS, mCustomerDetail);
				setResult(ComConst.CUSTOMER_CLASSIFY, intent);
				imgbtnHome.setVisibility(View.INVISIBLE);
				finish();
				slideReturnActivity();
			}
		});
	}

	public void initDatas() {
		mCustomerDetail = (CustomerDetail) getIntent().getSerializableExtra(
				ComConst.CUSTOMER_DATAS);
		mActivityStatus = getIntent().getIntExtra(
				ComConst.CUSTOMER_ACTIVITY_STATUS, 0);

		if (mCustomerDetail.CustomerType != null) {
			displayDatas();
		} else {
			mCustomerDetail = new CustomerDetail();
		}

		setTitle(getResources().getString(string.customer_title_classify));
	}

	private void displayDatas() {
		ivClassify1.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify2.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify3.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify4.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify5.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify6.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify7.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify8.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify9.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify10.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		ivClassify11.setImageDrawable(getResources().getDrawable(
				drawable.unchecked));
		linearClassify4Time.setVisibility(View.GONE);
		linearClassify5PayTime.setVisibility(View.GONE);
		linearClassify5NextPayTime.setVisibility(View.GONE);
		int tag = Integer.valueOf(mCustomerDetail.CustomerType);
		switch (tag) {
		case 1:
			ivClassify1.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 2:
			ivClassify2.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 3:
			ivClassify3.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 4:
			linearClassify4Time.setVisibility(View.VISIBLE);
			ivClassify4.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			etClassify4Time.setText(dateFormat
					.format(mCustomerDetail.CustomerTypeTime1));
			break;
		case 5:
			linearClassify5PayTime.setVisibility(View.VISIBLE);
			linearClassify5NextPayTime.setVisibility(View.VISIBLE);
			ivClassify5.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			etClassify5PayTime.setText(dateFormat
					.format(mCustomerDetail.CustomerTypeTime1));
			etClassify5NextPayTime.setText(dateFormat
					.format(mCustomerDetail.CustomerTypeTime2));
			break;
		case 6:
			ivClassify6.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 7:
			ivClassify7.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 8:
			ivClassify8.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 9:
			ivClassify9.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 10:
			ivClassify10.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		case 11:
			ivClassify11.setImageDrawable(getResources().getDrawable(
					R.drawable.checked));
			break;
		default:
			break;
		}
	}

	private void getDatas() {
		try {
			if ("4".equals(mCustomerDetail.CustomerType)) {
				mCustomerDetail.CustomerTypeTime1 = dateFormat
						.parse(etClassify4Time.getText().toString());
			} else if ("5".equals(mCustomerDetail.CustomerType)) {
				mCustomerDetail.CustomerTypeTime1 = dateFormat
						.parse(etClassify5PayTime.getText().toString());
				mCustomerDetail.CustomerTypeTime2 = dateFormat
						.parse(etClassify5NextPayTime.getText().toString());
			}
		} catch (Exception e) {

		}
	}

	/**
	 * 禁止EditText弹出虚拟键盘
	 * 
	 * @param et
	 */
	private void setEditTextForbiddenVirtualKeyBoard(final EditText et) {
		et.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 设置点击EditText不弹出系统的虚拟键盘
				// backup the input type
				int inType = et.getInputType();
				// disable soft input
				et.setInputType(InputType.TYPE_NULL);
				// call native handler
				et.onTouchEvent(event);
				// restore input type
				et.setInputType(inType); // restore input type
				et.setSelection(et.getText().length());
				return true;
			}
		});
	}

	private void setImageViewListener(final ImageView iv) {
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				ivClassify1.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify2.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify3.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify4.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify5.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify6.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify7.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify8.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify9.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify10.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				ivClassify11.setImageDrawable(getResources().getDrawable(
						drawable.unchecked));
				linearClassify4Time.setVisibility(View.GONE);
				linearClassify5PayTime.setVisibility(View.GONE);
				linearClassify5NextPayTime.setVisibility(View.GONE);

				try {
					if (iv.getDrawable()
							.getConstantState()
							.equals(getResources().getDrawable(
									R.drawable.unchecked).getConstantState())) {
						// 设置图片
						iv.setImageDrawable(getResources().getDrawable(
								R.drawable.checked));
						if (iv.getId() == id.iv_classify4) {
							linearClassify4Time.setVisibility(View.VISIBLE);
						} else if (iv.getId() == id.iv_classify5) {
							linearClassify5PayTime.setVisibility(View.VISIBLE);
							linearClassify5NextPayTime
									.setVisibility(View.VISIBLE);
						}
						switch (iv.getId()) {
						case id.iv_classify1:
							mCustomerDetail.CustomerType = "1";
							break;
						case id.iv_classify2:
							mCustomerDetail.CustomerType = "2";
							break;
						case id.iv_classify3:
							mCustomerDetail.CustomerType = "3";
							break;
						case id.iv_classify4:
							mCustomerDetail.CustomerType = "4";
							break;
						case id.iv_classify5:
							mCustomerDetail.CustomerType = "5";
							break;
						case id.iv_classify6:
							mCustomerDetail.CustomerType = "6";
							break;
						case id.iv_classify7:
							mCustomerDetail.CustomerType = "7";
							break;
						case id.iv_classify8:
							mCustomerDetail.CustomerType = "8";
							break;
						case id.iv_classify9:
							mCustomerDetail.CustomerType = "9";
							break;
						case id.iv_classify10:
							mCustomerDetail.CustomerType = "10";
							break;
						case id.iv_classify11:
							mCustomerDetail.CustomerType = "11";
							break;
						default:
							break;
						}
					} else {
						// 设置图片
						iv.setImageDrawable(getResources().getDrawable(
								R.drawable.unchecked));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

		});
	}

	private void setTimeClickListener(final ImageView iv) {
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				LayoutInflater inflater = LayoutInflater
						.from(CustomerClassify.this);
				final View timepickerview = inflater.inflate(
						R.layout.item_time_picker, null);
				ScreenInfo screenInfo = new ScreenInfo(CustomerClassify.this);
				wheelMain = new WheelMain(timepickerview);
				wheelMain.screenheight = screenInfo.getHeight();
				String time = "";
				switch (arg0.getId()) {
				case R.id.iv_date_picker1:
					time = etClassify4Time.getText().toString();
					break;
				case R.id.iv_date_picker2:
					time = etClassify5PayTime.getText().toString();
					break;
				case R.id.iv_date_picker3:
					time = etClassify5NextPayTime.getText().toString();
					break;
				default:
					break;
				}
				Calendar calendar = Calendar.getInstance();
				// 显示时间
				if (JudgeDate.isDate(time, "yyyy-MM-dd")) {
					try {
						calendar.setTime(dateFormat.parse(time));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				wheelMain.initDateTimePicker(year, month, day);

				new AlertDialog.Builder(CustomerClassify.this)
						.setTitle("时间选择")
						.setView(timepickerview)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										switch (iv.getId()) {
										case R.id.iv_date_picker1:
											etClassify4Time.setText(wheelMain
													.getTime());
											Log.d("----------->",
													wheelMain.getTime());
											break;
										case R.id.iv_date_picker2:
											etClassify5PayTime
													.setText(wheelMain
															.getTime());
											break;
										case R.id.iv_date_picker3:
											etClassify5NextPayTime
													.setText(wheelMain
															.getTime());
											break;
										default:
											break;
										}
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
									}
								}).show();
			}
		});
	}
}
